package com.ruoyi.iot.dataprocessing.enums;


import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.PrimaryKeyIdUtils;
import com.ruoyi.iot.dataprocessing.domain.SensorMessage;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.Date;
import java.util.function.Function;

@Getter
@AllArgsConstructor
public enum DataAssemblyEnum {
    DEFAULT(0,"默认", getSensorMessageFunction()),
    OXIMETER(1,"血氧仪", getSensorMessageFunction()),
    BLOOD_PRESSURE_MONITOR(2,"血压计",getSensorMessageFunction()),
    SPHYGMOSCOPE(3,"脉诊仪",getSensorMessageFunction()),
    WATCH(4,"手表",getSensorMessageFunction2Watch());

    private static Function<byte[], SensorMessage> getSensorMessageFunction() {
        return payload -> {
            JSONObject jsonObject = JSONObject.parseObject(new String(payload));
            SensorMessage sensorMessage = new SensorMessage();
            sensorMessage.setId(PrimaryKeyIdUtils.getSysIdWorker().nextId());
            sensorMessage.setDeviceType(jsonObject.getIntValue("type"));
            sensorMessage.setDeviceNo(jsonObject.getString("deviceId"));
            sensorMessage.setCustomerId(jsonObject.getLong("userId"));
            sensorMessage.setMessage(payload);
            sensorMessage.setTsStart(new Date(jsonObject.getLong("ts")));
            sensorMessage.setTsEnd(new Date(jsonObject.getLong("ts")));
            sensorMessage.setCreateTime(DateUtils.getNowDate());
            return sensorMessage;
        };
    }

    private static Function<byte[], SensorMessage> getSensorMessageFunction2Watch() {
        return payload -> {
            JSONObject jsonObject = JSONObject.parseObject(new String(payload));
            SensorMessage sensorMessage = new SensorMessage();
            sensorMessage.setId(PrimaryKeyIdUtils.getSysIdWorker().nextId());
            sensorMessage.setDeviceType(jsonObject.getIntValue("type"));
            sensorMessage.setDeviceNo(jsonObject.getString("mac"));
            sensorMessage.setCustomerId(jsonObject.getLong("userId"));
            sensorMessage.setMessage(payload);
            sensorMessage.setTsStart(new Date(jsonObject.getLong("ts")));
            sensorMessage.setTsEnd(new Date(jsonObject.getLong("ts")));
            sensorMessage.setCreateTime(DateUtils.getNowDate());
            return sensorMessage;
        };
    }

    private final Integer deviceType;
    private final String name;
    private final Function<byte[], SensorMessage> handle;

    public static SensorMessage getSensorMessageByPaload(Integer deviceType, byte[] payload) {
        DataAssemblyEnum dataAssemblyEnum = Arrays.stream(DataAssemblyEnum.values()).filter(item -> deviceType.equals(item.getDeviceType())).findFirst().orElse(null);
       // return Optional.ofNullable(dataAssemblyEnum).map(DataAssemblyEnum::getDeviceType).orElse(null);
        return dataAssemblyEnum != null?dataAssemblyEnum.getHandle().apply(payload):null;
    }
    public static DataAssemblyEnum getEnum(Integer deviceType) {
        DataAssemblyEnum dataAssemblyEnum = Arrays.stream(DataAssemblyEnum.values()).filter(item -> deviceType.equals(item.getDeviceType())).findFirst().orElse(null);
       // return Optional.ofNullable(dataAssemblyEnum).map(DataAssemblyEnum::getDeviceType).orElse(null);
        return dataAssemblyEnum != null?dataAssemblyEnum:DEFAULT;
    }

}
